//
//  SAUpdateShopPriceView.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/28.
//

#import "SAUpdateShopPriceView.h"
#import "SAUpdateShopPriceCell.h"
#import "SAProductOverModel.h"
#import "SABatchPriceAPI.h"

@interface SAUpdateShopPriceView ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSMutableArray *leftMarr;
@property (nonatomic, strong) NSMutableArray *rightMarr;
@property (nonatomic, strong) NSMutableArray *typeMarr;
@property (nonatomic, strong) SABatchPriceAPI *batchPriceAPI;

@property (nonatomic, strong) UITextField *wholesaleMoneyTextfiled;
@property (nonatomic, strong) UITextField *retailMoneyTextfiled;
@property (nonatomic, strong) UITextField *doorMoneyTextfiled;
@property (nonatomic, strong) UITextField *addandSubtractTextfiled;
@property (nonatomic, strong) UITextField *takeTextfiled;

@end

@implementation SAUpdateShopPriceView

+(instancetype)shareCornView {
    SAUpdateShopPriceView * shareView = [[SAUpdateShopPriceView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
        [self initUI];
    }
    return self;
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

#pragma mark - event
-(void)batchPrice {
    
}

#pragma mark - initUI
-(void)initUI {
    
    [self addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScaleNum(500));
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(500), kScreenWidth, kScaleNum(500))];
    backGroundView.backgroundColor = kLineColor;
    backGroundView.userInteractionEnabled = YES;
    [self.headerView addSubview:backGroundView];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(kScreenWidth - kScaleNum(80), kScaleNum(0), kScaleNum(80), kScaleNum(32));
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmationTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:confirmBtn];
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(32), kScreenWidth, kScaleNum(32))];
    whiteView2.backgroundColor = [UIColor whiteColor];
    [backGroundView addSubview:whiteView2];
    //零售价:
    UILabel *retailMoneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(30), kScaleNum(0), kScaleNum(120), kScaleNum(32))];
    retailMoneyLabel.textAlignment = NSTextAlignmentLeft;
    retailMoneyLabel.textColor = kBlackColor;
    retailMoneyLabel.font = kFont(14);
    retailMoneyLabel.text = @"零售价";
    [whiteView2 addSubview:retailMoneyLabel];
    
    UITextField *retailMoneyTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(150), kScaleNum(0), kScaleNum(195), kScaleNum(32))];
    retailMoneyTextfiled.delegate = self;
    retailMoneyTextfiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
    retailMoneyTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    retailMoneyTextfiled.font = kFont(14);
    retailMoneyTextfiled.keyboardType = UIKeyboardTypeDecimalPad;
    retailMoneyTextfiled.placeholder = @"请输入零售价";
    retailMoneyTextfiled.textAlignment = NSTextAlignmentRight;
    [whiteView2 addSubview:retailMoneyTextfiled];
    self.retailMoneyTextfiled = retailMoneyTextfiled;
    retailMoneyTextfiled.textColor = kGrayColor;
    
    UIView *whiteView4 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(74), kScreenWidth, kScaleNum(32))];
    whiteView4.backgroundColor = [UIColor whiteColor];
    [backGroundView addSubview:whiteView4];
    
    UILabel *addandSubtractLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(30), kScaleNum(0), kScaleNum(100), kScaleNum(32))];
    addandSubtractLabel.textAlignment = NSTextAlignmentLeft;
    addandSubtractLabel.textColor = kGrayColor;
    addandSubtractLabel.font = kFont(14);
    addandSubtractLabel.text = @"统一加减";
    [whiteView4 addSubview:addandSubtractLabel];
    
    UITextField *addandSubtractTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(150), kScaleNum(0), kScaleNum(195), kScaleNum(32))];
    addandSubtractTextfiled.placeholder = @"填“-8”表示在原价格基础上减8";
    addandSubtractTextfiled.delegate = self;
    addandSubtractTextfiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
    addandSubtractTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    addandSubtractTextfiled.font = kFont(14);
    addandSubtractTextfiled.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [whiteView4 addSubview:addandSubtractTextfiled];
    addandSubtractTextfiled.textAlignment = NSTextAlignmentRight;
    self.addandSubtractTextfiled = addandSubtractTextfiled;
    addandSubtractTextfiled.textColor = kGrayColor;
    
    UIView *whiteView5 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(116), kScreenWidth, kScaleNum(32))];
    whiteView5.backgroundColor = [UIColor whiteColor];
    [backGroundView addSubview:whiteView5];
    
    UILabel *takeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(30), kScaleNum(0), kScaleNum(100), kScaleNum(32))];
    takeLabel.textAlignment = NSTextAlignmentLeft;
    takeLabel.textColor = kGrayColor;
    takeLabel.font = kFont(14);
    takeLabel.text = @"统一乘";
    [whiteView5 addSubview:takeLabel];
    
    UITextField *takeTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(150), kScaleNum(0), kScaleNum(195), kScaleNum(32))];
    takeTextfiled.placeholder = @"填“0.8”表示在原价基础上打8折";
    takeTextfiled.delegate = self;
    takeTextfiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
    takeTextfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    takeTextfiled.font = kFont(14);
    takeTextfiled.keyboardType = UIKeyboardTypeDecimalPad;
    [whiteView5 addSubview:takeTextfiled];
    takeTextfiled.textAlignment = NSTextAlignmentRight;
    self.takeTextfiled = takeTextfiled;
    takeTextfiled.textColor = kGrayColor;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField == self.wholesaleMoneyTextfiled || textField == self.retailMoneyTextfiled || textField == self.doorMoneyTextfiled) {
        if (trimString(self.addandSubtractTextfiled.text).length >0 ||trimString(self.takeTextfiled.text).length >0) {
            return NO;
        }else
        {
            return YES;
        }
    }else if (textField == self.addandSubtractTextfiled)
    {
        if (trimString(self.wholesaleMoneyTextfiled.text).length >0 ||trimString(self.retailMoneyTextfiled.text).length >0||trimString(self.doorMoneyTextfiled.text).length >0||trimString(self.takeTextfiled.text).length >0) {
            return NO;
            
        }else
        {
            return YES;
            
        }
    }else if (textField == self.takeTextfiled)
    {
        if (trimString(self.wholesaleMoneyTextfiled.text).length >0 ||trimString(self.retailMoneyTextfiled.text).length >0||trimString(self.doorMoneyTextfiled.text).length >0||trimString(self.addandSubtractTextfiled.text).length >0) {
            return NO;
            
        }else
        {
            return YES;
            
        }
    }
    return YES;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 7) {
        return NO;
    }
    if (self.retailMoneyTextfiled == textField) {
        if ([string isEqualToString:@"."]) {
            return NO;
        }
    }
    
    if ([string isEqualToString:@"-"]) {
        NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];
        NSRange range2 = [textField.text rangeOfCharacterFromSet:tmpSet];
        if (range2.length > 0) {
            return NO;
        }
    }

    if (textField == self.wholesaleMoneyTextfiled || textField == self.retailMoneyTextfiled || textField == self.doorMoneyTextfiled || textField == self.takeTextfiled) {
        return [self validateNumber:string];
    }
    
    if (textField == self.addandSubtractTextfiled ) {
        return [self validateNumber1:string];
    }
    
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (BOOL)validateNumber1:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"-0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)showView:(UIViewController *)Sview {
    if(Sview == nil){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }else{
        [Sview.view addSubview:self];
    }
}

-(void)confirmationTapEvent {
    [self endEditing:YES];
    if ( trimString(self.wholesaleMoneyTextfiled.text).length > 0 || trimString(self.retailMoneyTextfiled.text).length > 0 || trimString(self.doorMoneyTextfiled.text).length > 0) {
        if (self.pricinSuccessBlock) {
            self.pricinSuccessBlock(trimString(self.wholesaleMoneyTextfiled.text), trimString(self.retailMoneyTextfiled.text), trimString(self.doorMoneyTextfiled.text), @"1");
        }
    }
    if (trimString(self.addandSubtractTextfiled.text).length > 0) {
        if (self.pricingSuccessBlock) {
            self.pricingSuccessBlock(trimString(self.addandSubtractTextfiled.text), @"2");
        }
    }
    if (trimString(self.takeTextfiled.text).length > 0) {
        if (self.pricingSuccessBlock) {
            self.pricingSuccessBlock(trimString(self.takeTextfiled.text), @"3");
        }
    }
    [self shareDissTapEvent];
}

-(void)shareDissTapEvent
{
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[SAUpdateShopPriceCell class] forCellReuseIdentifier:@"SAUpdateShopPriceCell"];
    }
    return _tableView;
}

-(SABatchPriceAPI *)batchPriceAPI {
    if (!_batchPriceAPI) {
        _batchPriceAPI = [SABatchPriceAPI apiWithDelegate:self];
    }
    return _batchPriceAPI;
}

-(UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kScreenWidth, kScreenHeight)];
    }
    return _headerView;
}

@end
